package com.bigdata.schaduler.service;

import com.bigdata.schaduler.client.DolphinClient;
import com.bigdata.schaduler.domain.ProcessStartParam;
import com.bigdata.schaduler.enums.ExecuteType;
import org.apache.dolphinscheduler.common.enums.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

public class ExecutorService {
  private final DolphinClient dolphinClient;

  public ExecutorService(DolphinClient dolphinClient) {
    this.dolphinClient = dolphinClient;
  }

  public String getProcessDefinitionUri(Long projectCode) {
    return "projects/" + projectCode + "/executors";
  }

  public void startProcessInstance(Long projectCode, ProcessStartParam startParam) {
    String uri = getProcessDefinitionUri(projectCode) + "/start-process-instance";
    dolphinClient.requestFormData(HttpMethod.POST, uri, startParam, Void.class);
  }

  public void execute(Long projectCode, Integer instanceId, ExecuteType executeType) {
    String uri = getProcessDefinitionUri(projectCode) + "/execute";
    MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    params.add("processInstanceId", instanceId);
    params.add("executeType", executeType.name());
    dolphinClient.requestFormData(HttpMethod.POST, uri, params, Void.class);
  }
}
